<template>
  <div>
    <!--   -->
    <div class="dev_box" v-if="control">
      <div>
        <div :class="objcontrol.dev_state == 1
          ? 'header_back3'
          : objcontrol.dev_state == 2
            ? 'header_back'
            : objcontrol.dev_state == 3
              ? 'header_back2'
              : 'header_back1'
          ">
          <div class="roomName">
            <span style="margin-left: 20px">{{ objcontrol.roomNum }}</span><span v-if="objcontrol.remark">-{{
              objcontrol.remark }}</span>
          </div>
          <div v-if="objcontrol.dev_state">
            {{
              objcontrol.dev_state == 1
              ? "正常"
              : objcontrol.dev_state == 2
                ? "报警"
                : objcontrol.dev_state == 3
                  ? "离线"
                  : "维修中"
            }}
          </div>
        </div>
        <echartcurve v-if="TotalEnergy" @handleClose="handleCloseTotalEnergy" :echart_curve="TotalEnergy"
          :valueList="yAxisList" :timeList="xAxisList" :echarts_name="'总能耗'"></echartcurve>
        <div class="content_info">
          <div v-if="objcontrol.dev_state == 1" :class="click_switch ? 'dev_boxback devbg' : ''"></div>

          <div class="auto-manual" style="justify-content: center">
            <el-select v-model="objcontrol.ctrlModel" :disabled="is_operate_permission == false" placeholder="请选择"
              @change="handleAutoManual($event, index, objcontrol)">
              <el-option v-for="item in auto_manual" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="auto-manual" style="justify-content: center">
            <el-input-number v-model="objcontrol.tempSet" :disabled="objcontrol.runState == 0 ||
              is_operate_permission == false ||
              handlecontrol
              " :max="30" :min="16" @change="handleChangeNum($event, objcontrol, index)"></el-input-number>
          </div>
          <div class="auto-manual">
            <div class="info-desc" @click="handleWorkingOperate(objcontrol, 7)">
              <i class="el-icon-switch-button" :style="objcontrol.acState == 0
                ? 'color:#c1c1c1'
                : 'color:rgb(18, 158, 41)'
                "></i>
              <div>开/关</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 1">
              <i class="el-icon-key" :style="objcontrol.runState == 0
                ? 'color:#c1c1c1'
                : 'color:rgb(18, 158, 41)'
                "></i>
              <div>温度自控</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 1">
              <i class="iconfont icon-icon-hongwaishuangjian" :style="objcontrol.bodySensor == 0
                ? 'color:#c1c1c1'
                : 'color:rgb(18, 158, 41)'
                "></i>
              <div>人体红外线</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 1">
              <i v-if="objcontrol.alarming" class="el-icon-warning-outline" style="color: rgb(18, 158, 41)"></i>
              <i v-else class="el-icon-warning-outline" style="color: #c1c1c1"></i>
              <div>异常警告</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 1" @click="handleseasonSet(objcontrol, index)">
              <i class="el-icon-sunny" style="color: rgb(18, 158, 41)"
                v-if="objcontrol.season == 1 || objcontrol.season == 0"></i>
              <i class="el-icon-cloudy-and-sunny" style="color: rgb(18, 158, 41)" v-if="objcontrol.season == 2"></i>
              <div v-if="objcontrol.season == 1">夏季</div>
              <div v-if="objcontrol.season == 2">冬季</div>
              <div v-if="objcontrol.season == 0">自动</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 1)">
              <i class="el-icon-rank" :style="objcontrol.runState == 2 && objcontrol.runMode == 1
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>制冷</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 4)">
              <i class="el-icon-sunset" :style="objcontrol.runState == 2 && objcontrol.runMode == 4
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>制热</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 3)">
              <i class="el-icon-orange" :style="objcontrol.runState == 2 && objcontrol.runMode == 3
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>通风</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 2)">
              <i class="el-icon-cloudy" :style="objcontrol.runState == 2 && objcontrol.runMode == 2
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>除湿</div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 6)">
              <i class="iconfont icon-fengxiang" :style="objcontrol.runState == 2 && objcontrol.windDirect > 0
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>
                风向<span v-if="objcontrol.windDirect > 0">{{
                  objcontrol.windDirect
                }}</span>
              </div>
            </div>
            <div class="info-desc" v-if="objcontrol.ctrlModel == 0" @click="handleWorkingOperate(objcontrol, 5)">
              <i class="el-icon-pie-chart" :style="objcontrol.runMode == 0 && objcontrol.runState == 2
                ? 'color:rgb(18, 158, 41)'
                : 'color:#c1c1c1'
                "></i>
              <div>自动</div>
            </div>
            <div class="info-desc" @click="handleRefresh(objcontrol, index)">
              <i class="el-icon-refresh" style="color: rgb(18, 158, 41)"></i>
              <div>刷新</div>
            </div>
          </div>
          <div class="auto-manual">
            <div class="manual">室内温度：{{ objcontrol.tempInfo }} ℃</div>
            <!-- 1.8新增 -->
            <div class="manual">
              运行状态：{{
                objcontrol.runState == 0
                ? "断电"
                : objcontrol.runState == 1
                  ? "已暂停"
                  : objcontrol.runState == 2
                    ? "运行中"
                    : ""
              }}
            </div>
            <!-- <div class="manual">今日用电时长：32 min</div> -->
          </div>
          <div class="auto-manual">
            <div class="manual">湿度：{{ objcontrol.humidity }}%rh</div>
            <div class="manual">总用电时长：{{ objcontrol.totalTime }}min</div>
          </div>
          <div class="auto-manual">
            <div class="manual">光照度：{{ objcontrol.illu }}%rh</div>
            <div class="manual" @click="handleTotalEnergy(item)">
              总能耗：<text class="colorblue">{{ objcontrol.totalElectricity || 0 }}KW.H</text>
            </div>
          </div>
          <div class="all_icon">
            <div class="switchbtn">
              <span style="margin-right: 10px; color: red">断电</span>
              <el-switch @change="handlePowerOperate($event, objcontrol, 1)" v-model="objcontrol.runStateSwitch"
                :active-value="1" :inactive-value="0" :disabled="dis_switch" active-color="#13ce66"
                inactive-color="rgb(158 158 158)" />
              <span style="margin-left: 5px; color: #10cc4e">通电</span>
            </div>
            <!-- <i class="el-icon-remove-outline"></i> -->
            <i class="el-icon-s-order" @click="handleRunningLog(objcontrol)"></i>
            <i class="el-icon-finished" v-if="is_editingOperation" @click="handleinfraredCtl(2, objcontrol, index)"></i>
            <i class="el-icon-setting" v-if="is_editingOperation" @click="handleparameterSet(2, objcontrol, index)"></i>
            <i class="el-icon-edit" v-if="is_editingOperation" @click="handleAdd(objcontrol, index)"></i>
            <i class="el-icon-delete" @click="handledelete" v-if="is_delete_permission"></i>
          </div>
        </div>
      </div>
      <i class="el-icon-close" @click="handlecloseControl"></i>
    </div>
    <addAir :editVisible="addPopup" v-if="addPopup" :RoomList="RoomList" @handleclose="handleclose"
      @handleclick="handleclickAdd" :campusList="campusList" @handleChangeAreaCodeEdit="handleChangeAreaCodeEdit"
      @handleAreaCode="handleAreaCode" ref="refAddAir" :add_form="add_form"></addAir>
    <!-- 红外线控制 -->
    <infrared :editVisible="infraredCtl" v-if="infraredCtl" :airList="selectList" :add_from="add_from_infraredRay"
      @handleclose="handleclose" @handleclick="saveEditInfraredCtl" @handleAreaCode="handleAreaCode"></infrared>

    <!--基础参数设置  -->
    <basicsSet :editVisible="parameterSet" v-if="parameterSet" :airList="selectList" :add_form="add_from_parameter.value"
      @handleclose="handleclose" @handleclick="saveEditParameterSet"></basicsSet>
    <!--  运行时间日志-->
    <air-RunningLog v-if="runningLog" :airLogItem="airLogItem" :runningLog="runningLog"
      @handleclose="handleclose"></air-RunningLog>
  </div>
</template>
<script>
import { ref, reactive, getCurrentInstance, nextTick } from "vue";
import addAir from "../components/addAir.vue";
import infrared from "../components/infrared.vue";
import basicsSet from "../components/basicsSet.vue";
import echartcurve from "../components/echart_curve.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import parsing from "../assets/js/parsing";
import { getinfluxData } from "../assets/js/getcampusList";
import api from "../api/api";
import airRunningLog from "../pages/deviceMgt/airRunningLog.vue";
export default {
  components: { addAir, infrared, basicsSet, echartcurve, airRunningLog },
  setup(props, context) {
    const addPopup = ref(false);
    const airId = ref([]);
    const objcontrol = context.attrs.objcontrol;
    let dev_Obj = {
      isOperate: objcontrol.isOperate,//维修
      isOnline: objcontrol.online,//是否离线
      alarmState: objcontrol.alarmState,//是否报警
    }
    // if (objcontrol.online) {
    //   dev_Obj.isOnline = 1;
    // } else {
    //   dev_Obj.isOnline = 0;
    // }
    objcontrol.dev_state = parsing.getAirdevstate(dev_Obj);
    console.log(objcontrol)
    let { proxy } = getCurrentInstance();
    // 报修列表权限
    // 编辑
    const is_editingOperation = ref(false);
    proxy.$permissionList("AIR_ALL_UPDATE").then((res) => {
      is_editingOperation.value = res;
    });
    // 面板操作
    const is_operate_permission = ref(false);
    // 空调库控制面板
    proxy.$permissionList("AIR_ALL_OPERATE").then((res) => {
      console.log(res)
      is_operate_permission.value = res;

    });
    if (!is_operate_permission.value) {
      // 教室管理设备操作面板
      proxy.$permissionList("CLASSROOM_DEVICEOPERATE").then((res1) => {
        console.log(res1)
        is_operate_permission.value = res1;
      });
    }
    if (!is_operate_permission.value) {
      // 空调管理控制面板
      proxy.$permissionList("AIR_MANAGE_OPERATE").then((res2) => {
        console.log(res2)
        is_operate_permission.value = res2;
      });
    }
    // 删除权限
    const is_delete_permission = ref(false);
    proxy.$permissionList("AIR_MANAGE_DELETE").then((res) => {
      is_delete_permission.value = res;
    });
    // 获取校区
    const campusList = ref([]);
    proxy.$branchOfficeTree().then((res) => {
      campusList.value = res;
    });
    const add_form = reactive({
      id: objcontrol.id,
      regionId: objcontrol.regionId,
      imei: objcontrol.imei,
      shunt: objcontrol.shunt,
      switchShunt: objcontrol.switchShunt,
      description: objcontrol.description,
      brandName: objcontrol.brandName,
      modelName: objcontrol.modelName,
      tempTop: objcontrol.tempTop,
      tempLowest: objcontrol.tempLowest,
      isCount: objcontrol.isCount,
      isOperate: objcontrol.isOperate,
    });
    const add_from_infraredRay = reactive({
      imei: objcontrol.imei,
      lineId: objcontrol.shunt,
      modelCode: objcontrol.modelCode,
      acState: null,
      runMode: objcontrol.runMode,
      setTemp: objcontrol.tempSet,
      windSpeed: objcontrol.windSpeed,
      windDirect: objcontrol.windDirect,
    });
    const add_from_parameter = reactive({
      value: {
        imei: null,
        lineId: null,
        seasonAuto: 0,
        summerTime: null,
        winterTime: null,
        delayOff: null,
        nobodyTime: null,
        acRunMA: null,
        autoInterval: null,
        modelId: null,
        yhModel: null,
        beepOn: null,
        rcPWD: null,
        ldrOn: null,
      },
    });

    const getShown = () => {
      setTimeout(function () {
        if (objcontrol.runState == 2) {
          add_from_infraredRay.acState = 1;
        } else {
          add_from_infraredRay.acState = 0;
        }
        add_from_infraredRay.imei = objcontrol.imei;
        add_from_infraredRay.lineId = objcontrol.shunt;
        add_from_infraredRay.modelCode = objcontrol.modelCode;
        add_from_infraredRay.runMode = objcontrol.runMode;
        add_from_infraredRay.setTemp = objcontrol.tempSet;
        add_from_infraredRay.windSpeed = objcontrol.windSpeed;
        add_from_infraredRay.windDirect = objcontrol.windDirect;
      }, 500);
    };
    getShown();
    const getConfig = () => {
      api
        .getListAPI(
          "backup/airConditionOperate/getConfig?imei=" +
          objcontrol.imei +
          "&shunt=" +
          objcontrol.shunt,
          {}
        )
        .then((res) => {
          if (res.data.code == 1) {
            selectList.value = [];
            add_from_parameter.value = res.data.data;
            add_from_parameter.value.imei = objcontrol.imei;
            add_from_parameter.value.lineId = objcontrol.shunt;
            // add_from_parameter.seasonAuto = res.data.data.seasonAuto;
            // add_from_parameter.winterTime = res.data.data.winterTime;
            // add_from_parameter.nobodyTime = res.data.data.nobodyTime;
            // add_from_parameter.summerTime = res.data.data.summerTime;
            // add_from_parameter.delayOff = res.data.data.delayOff;
            // add_from_parameter.acRunMA = res.data.data.acRunMA;
            // add_from_parameter.autoInterval = res.data.data.autoInterval;
            // add_from_parameter.beepOn = res.data.data.beepOn;
            // add_from_parameter.rcPWD = res.data.data.rcPWD;
            // add_from_parameter.ldrOn = res.data.data.ldrOn;
            // add_from_parameter.modelId = res.data.data.modelId;
            // add_from_parameter.yhModel = res.data.data.yhModel;
            parameterSet.value = true;
            selectList.value.push(add_from_parameter.value);
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => { });
    };
    const RoomList = ref([]);
    const refAddAir = ref();
    const getRoomList = () => {
      api
        .getListAPI("backup/region/list?areaCode=" + objcontrol.areaCode, {})
        .then((res) => {
          if (res.data.code == 1) {
            RoomList.value = res.data.data;
            refAddAir.value.RoomList = res.data.data;
          } else {
            ElMessage.error(res.data.message);
          }
          //数据处理
        })
        .catch((err) => {
          console.log(err);
        });
    };
    const handleAdd = (row) => {
      addPopup.value = true;
      console.log(row);
      airId.value.push(objcontrol);
      if (row.regionId) {
        add_form.areaCode = row.areaCode.substring(0, 8);
        add_form.buildAreaCode = row.areaCode;
        EditQueryAreaCode.value = row.areaCode;
        proxy.$listExist(add_form.areaCode).then((res) => {
          refAddAir.value.parent_category = res;
        });
      }
      if (row.id) {
        api
          .getListAPI("backup/airCondition/detail?id=" + row.id, {})
          .then((res) => {
            if (res.data) {
              let obj = res.data.data;
              add_form.id = obj.id;
              add_form.regionId = obj.regionId;
              add_form.imei = obj.imei;
              add_form.shunt = obj.shunt;
              add_form.switchShunt = obj.switchShunt;
              add_form.description = obj.description;
              add_form.brandName = obj.brandName;
              add_form.modelName = obj.modelName;
              add_form.tempTop = obj.tempTop;
              add_form.tempLowest = obj.tempLowest;
              add_form.tempTop = obj.tempTop;
              add_form.isCount = obj.isCount;
              add_form.isOperate = obj.isOperate;
            } else {
              ElMessage.error(res.data.message);
            }
          })
          .catch((err) => { });
      } else {
        add_form.id = null;
        add_form.regionId = null;
        add_form.imei = null;
        add_form.shunt = null;
        add_form.switchShunt = null;
        add_form.description = null;
        add_form.brandName = null;
        add_form.modelName = null;
        add_form.tempTop = null;
        add_form.tempLowest = null;
        add_form.tempTop = null;
        add_form.isCount = 1;
      }
      getRoomList();
    };
    const EditQueryAreaCode = ref("");
    const handleChangeAreaCodeEdit = (res) => {
      EditQueryAreaCode.value = null;
      if (res) {
        if (res && res.length > 1) {
          var index = res.length - 1;
          EditQueryAreaCode.value = res[index];
          add_form.buildAreaCode = res[index];
        } else {
          EditQueryAreaCode.value = res[0];
        }
      }
      getRoomList();
    };
    const handleAreaCode = (res) => {
      var index = "";
      if (res && res.length > 1) {
        index = res.length - 1;
        query.queryAreaCode = res[index];
      }
      console.log(res[index]);
      if (res[index].length > 6) {
        proxy.$listExist(res[index]).then((res) => {
          refAddAir.value.parent_category = res;
        });
      }
      add_form.regionId = null;
    };
    const control = ref(context.attrs.control);
    const selectList = ref([]);
    // 红外控制
    const infraredCtl = ref(false);
    const handleinfraredCtl = () => {
      selectList.value = [];
      infraredCtl.value = true;
    };
    // 参数设置
    const parameterSet = ref(false);
    const handleparameterSet = () => {
      getConfig();
    };
    const handleclose = (row) => {
      addPopup.value = row;
      infraredCtl.value = row;
      parameterSet.value = row;
      runningLog.value = row;
    };
    // 选择模式
    const pattern = ref("");
    const selfMotion = ref(0);
    const handleAutoManual = (e, index) => {
      selfMotion.value = e;
      objcontrol.ctrlModel = e;
      api
        .postFormAPI("backup/airConditionOperate/ctrlModel", {
          imei: objcontrol.imei,
          lineId: Number(objcontrol.shunt),
          ctrlModel: e,
        })
        .then((res) => {
          if (res.data.code == 1) {
            getRefresh(objcontrol, index);
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => { });
    };
    const saveEditParameterSet = (form, e) => {
      console.log(form);
      parameterSet.value = false;
      selectList.value = [];
      settingparameter(form);
    };
    const settingparameter = (Operate) => {
      if (Operate.seasonAuto == 2 || Operate.seasonAuto == 1) {
        Operate.summerTime = null;
        Operate.winterTime = null;
      }
      api
        .postFormAPI("backup/airConditionOperate/setConfig", Operate)
        .then((res) => {
          if (res.data.code == 1) {
            ElMessage.success("设置成功！");
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => { });
    };
    // 确定红外线控制
    const saveEditInfraredCtl = (form) => {
      console.log(form);
      settingPattern(form);
      selectList.value = [];
    };
    const settingPattern = (Operate) => {
      api
        .postFormAPI("backup/airConditionOperate/workingOperate", Operate)
        .then((res) => {
          if (res.data.code == 1) {
            infraredCtl.value = false;
            ElMessage.success("设置成功！");
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => { });
    };
    const NumberValue = ref(1);
    // 确定添加
    const handleclickAdd = (form, e) => {
      if (!form.regionId) {
        ElMessage.error("请选择房间号");
        return;
      } else if (!form.imei) {
        ElMessage.error("请输入设备");
        return;
      } else if (!form.shunt) {
        ElMessage.error("请输入分路号");
        return;
      }
      api
        .postFormAPI("backup/airCondition/update", form)
        .then((res) => {
          if (res.data.code == 1) {
            ElMessage.success("修改成功！");
            addPopup.value = false;
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => { });
    };
    // 删除
    const handledelete = () => {
      context.emit("handledelete", objcontrol);
    };
    const handlecloseControl = () => {
      context.emit("handlecloseControl", false);
    };
    // 空调电源开关操作
    const dis_switch = ref(false);
    const isonLoad = ref(0);
    const isinitial = ref(false);
    const handlePowerOperate = (event, item, index) => {
      console.log(is_operate_permission.value)
      let time = 0;
      let terval = null;
      if (isonLoad.value > 0 || isinitial.value == false) {
        isinitial.value = false;
        if (!is_operate_permission.value) {
          dis_switch.value = true;
          ElMessage.error("无操作权限");
          return;
        }
        dis_switch.value = true;
        var devState = 0;
        if (event == 0) {
          devState = 1;//分闸---断电
        } else {
          devState = 0;//合闸 ---通电
        }
        api
          .postFormAPI("backup/airConditionOperate/powerOperate", {
            imei: item.imei,
            lineId: Number(item.shunt),
            devState: devState,
          })
          .then((res) => {
            if (res.data.code == 1) {
              terval = setInterval(function () {
                if (time > 3) {
                  clearInterval(terval);
                }
                getRefresh(item, index);
                time++;
              }, 3000);
            } else {
              ElMessage.error(res.data.message);
            }
          })
          .catch((err) => { });
        setTimeout(function () {
          dis_switch.value = false;
        }, 3000);
      }
      isinitial.value = true;
      isonLoad.value++;
    }; // 刷新
    const handleRefresh = (item, index) => {
      getRefresh(item, index, 1);
      // api
      //   .getListAPI("backup/airCondition/detail?id=" + item.id, "", "GET")
      //   .then((res) => {
      //     objcontrol.dev_state = parsing.getAirdevstate(res.data.data);
      //   });
    };
    const getRefresh = (item, index, type) => {
      api
        .getListAPI(
          "backup/airConditionOperate/getShownSync?imei=" +
          item.imei +
          "&shunt=" +
          item.shunt,
          "",
          "GET"
        )
        .then((resobj) => {
          console.log(resobj);
          if (resobj.data.code == 1) {
            // 红外 runState等于2 acState为1
            objcontrol.acState = resobj.data.data.runState == 2 ? 1 : 0;
            // 运行状态 电源 :0:分闸状态 1:合闸供电状态但空调未运行2:空调运行中
            objcontrol.runState = resobj.data.data.runState;
            //  活体感应 1-有人 0-无人
            objcontrol.bodySensor = resobj.data.data.bodySensor;
            //  0-夏季(制冷) 1-冬季(制热)
            objcontrol.season = resobj.data.data.season;
            //用电时长,单位 分钟
            objcontrol.totalTime = resobj.data.data.totalTime;
            //工作模式 1-制冷 2-抽湿 3-送风 4-制热
            objcontrol.runMode = resobj.data.data.runMode;
            //风速 0:自动 1:风速1 2:风速2 3:风速3
            objcontrol.windSpeed = resobj.data.data.windSpeed;
            //风向 null-不动(停止,没有指定风向) 0:自动 1-风向1 2-风向2 3-风向3 4-风向4
            objcontrol.windDirect = resobj.data.data.windDirect;
            // 室内温度,单位℃
            objcontrol.tempInfo = resobj.data.data.tempInfo;
            // 湿度,单位%RH
            objcontrol.humidity = resobj.data.data.humidity;
            //光照度
            objcontrol.illu = resobj.data.data.illu;
            // 温度
            objcontrol.tempSet = resobj.data.data.tempSet;
            //异常
            objcontrol.alarming = resobj.data.data.alarming;
            objcontrol.switchInfo = resobj.data.data.switchInfo;
            objcontrol.ctrlModel = resobj.data.data.ctrlModel;
            let dev_Obj = {
              isOperate: item.isOperate,//维修
              isOnline: resobj.data.data.online,//是否离线
              alarmState: item.alarmState,//是否报警
            }
            // if (resobj.data.data.online) {
            //   dev_Obj.isOnline = 1;
            // } else {
            //   dev_Obj.isOnline = 0;
            // }
            objcontrol.dev_state = parsing.getAirdevstate(dev_Obj);
            if (type == 1) {
              ElMessage.success("已刷新");
            }
          } else {
            ElMessage.error(resobj.data.message);
          }
        })
        .catch((err) => { });
    };
    const WorkingOperate = reactive({
      runMode: 0,
      imei: null,
      lineId: 0,
      modelCode: null,
      acState: 0,
      setTemp: 0,
      windSpeed: 0,
      windDirect: 0,
    });
    const tervalTime = ref(0);
    const terval = ref(null);
    const isClick = ref(true);
    const click_switch = ref(false);
    // 设置空调模式
    const handleWorkingOperate = (item, index) => {
      console.log(index);
      if (click_switch.value) {
        return;
      }
      click_switch.value = true;
      if (!is_operate_permission.value) {
        ElMessage.error("无操作权限");
        return;
      }
      // index 1 制冷  2除湿 3送风 4制热 5自动 6风向 7开关
      WorkingOperate.windSpeed = item.windSpeed || 0;
      WorkingOperate.windDirect = item.windDirect || 0;
      if (index == 1) {
        if (item.runMode == 1 && item.runState == 2) {
          WorkingOperate.acState = 0; //制冷开关
        } else {
          WorkingOperate.acState = 1;
        }
        WorkingOperate.runMode = 1;
      } else if (index == 2) {
        if (item.runMode == 2 && item.runState == 2) {
          WorkingOperate.acState = 0; //除湿开关
        } else {
          WorkingOperate.acState = 1;
        }
        WorkingOperate.runMode = 2;
      } else if (index == 3) {
        if (item.runMode == 3 && item.runState == 2) {
          WorkingOperate.acState = 0; //送风开关
        } else {
          WorkingOperate.acState = 1;
        }
        WorkingOperate.runMode = 3;
      } else if (index == 4) {
        if (item.runMode == 4 && item.runState == 2) {
          WorkingOperate.acState = 0; //送风开关
        } else {
          WorkingOperate.acState = 1;
        }
        WorkingOperate.runMode = 4;
      } else if (index == 5) {
        if (item.runMode == 0 && item.runState == 2) {
          WorkingOperate.acState = 0; //自动
        } else {
          WorkingOperate.acState = 1;
        }
        WorkingOperate.runMode = 0;
        WorkingOperate.windSpeed = 0;
        WorkingOperate.windDirect = 0;
      } else if (index == 6) {
        // 风向
        // 风向
        // if (item.windDirect == 4) {
        WorkingOperate.windDirect = 0;
        // } else {
        //   WorkingOperate.windDirect = Number(WorkingOperate.windDirect) + 1;
        // }
      } else if (index == 7) {
        // 开关
        if (item.acState == 0) {
          WorkingOperate.acState = 1;
        } else {
          WorkingOperate.acState = 0;
        }
      }
      WorkingOperate.imei = item.imei;
      WorkingOperate.lineId = item.shunt;
      WorkingOperate.modelCode = item.modelCode;
      WorkingOperate.setTemp = item.tempSet || 22;
      if (item.runState == 0) {
        ElMessage.error("空调未开启");
      } else {
        settingPattern(WorkingOperate);
        if (isClick.value) {
          isClick.value = false;
          terval.value = setInterval(function () {
            console.log(tervalTime.value);
            if (tervalTime.value > 2) {
              clearInterval(terval.value);
              isClick.value = true;
              tervalTime.value = 0;
            }
            getRefresh(item, index);
            tervalTime.value++;
          }, 3000);
        } else {
          clearInterval(terval.value);
          tervalTime.value = 0;
          isClick.value = true;
          setTimeout(function () {
            getRefresh(item, index);
          }, 5000);
        }
      }
      setTimeout(function () {
        click_switch.value = false;
      }, 2000);
    };
    const handlecontrol = ref(false);
    const handleChangeNum = (res, item, index) => {
      handlecontrol.value = true;
      WorkingOperate.acState = 1;
      WorkingOperate.windSpeed = item.windSpeed;
      WorkingOperate.windDirect = item.windDirect;
      WorkingOperate.imei = item.imei;
      WorkingOperate.lineId = item.shunt;
      WorkingOperate.modelCode = item.modelCode;
      if (item.runState == 0) {
        ElMessage.error("空调未开启");
      } else {
        WorkingOperate.setTemp = res;
        settingPattern(WorkingOperate);
        getRefresh(item, index);
      }
      setTimeout(function () {
        handlecontrol.value = false;
      }, 2000);
    };
    // 季节切换
    const handleseasonSet = (item, index) => {
      let season = 0;
      if (item.runState != 0) {
        if (item.season == 0) {
          season = 1;
        } else if (item.season == 1) {
          season = 2;
        } else if (item.season == 2) {
          season = 0;
        }
        api
          .postFormAPI("backup/airConditionOperate/seasonSet", {
            imei: item.imei,
            lineId: Number(item.shunt),
            season: season,
          })
          .then((res) => {
            if (res.data.code == 1) {
            } else {
              ElMessage.error(res.data.message);
            }
          })
          .catch((err) => { });
      }
    };
    const TotalEnergy = ref(false);
    const yAxisList = ref([]);
    const xAxisList = ref([]);
    const handleTotalEnergy = () => {
      yAxisList.value = [];
      xAxisList.value = [];
      TotalEnergy.value = false;
      getinfluxData(
        "epTotal",
        objcontrol.switchInfo.devType,
        objcontrol.switchInfo.shunt,
        objcontrol.imei,
        "",
        ""
      ).then((res) => {
        for (var i = 0; i < res.length; i++) {
          yAxisList.value.push(res[i].value);
          xAxisList.value.push(res[i].time);
        }
        nextTick(function () {
          TotalEnergy.value = true;
        });
      });
    };
    const handleCloseTotalEnergy = () => {
      nextTick(function () {
        TotalEnergy.value = false;
      });
    };
    const runningLog = ref(false);
    const airLogItem = ref({});
    const handleRunningLog = (item) => {
      airLogItem.value = item;
      runningLog.value = true;
    };
    return {
      click_switch,
      handlecontrol,
      airLogItem,
      runningLog,
      proxy,
      TotalEnergy,
      yAxisList,
      xAxisList,
      refAddAir,
      is_editingOperation,
      is_operate_permission,
      is_delete_permission,
      add_from_infraredRay,
      add_from_parameter,
      RoomList,
      add_form,
      control,
      objcontrol,
      NumberValue,
      selectList,
      selfMotion,
      pattern,
      addPopup,
      infraredCtl,
      parameterSet,
      airId,
      dis_switch,
      WorkingOperate,
      campusList,
      handleRefresh,
      handlePowerOperate,
      handledelete,
      handleclickAdd,
      handleinfraredCtl,
      handleparameterSet,
      handleAdd,
      handleclose,
      handleAutoManual,
      saveEditParameterSet,
      saveEditInfraredCtl,
      handlecloseControl,
      handleWorkingOperate,
      handleChangeNum,
      handleseasonSet,
      handleTotalEnergy,
      handleCloseTotalEnergy,
      handleAreaCode,
      handleChangeAreaCodeEdit,
      handleRunningLog,
      auto_manual: [
        {
          value: 1,
          label: "变频模式",//自控模式
        },
        {
          value: 0,
          label: "手动模式",
        },
      ],
    };
  },
};
</script>
<style lang="less" scoped>
@import "../assets/css/common.css";
@import "../assets/css/device.less";
@import "../assets/fonts/iconfont.css";

.auto-manual {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  font-size: 14px;

  .manual {
    width: 40%;
    line-height: 40px;
    padding-left: 20px;
  }

  &:deep(.el-input--suffix .el-input__inner) {
    // width: 100px;
    border: 0px !important;
  }
}

.container {
  position: relative;
  z-index: 99;
}

.header_back2,
.header_back,
.header_back1 {
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

.content_info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1rem 0 1.5rem 0;
  position: relative;
}

.header_back2,
.header_back,
.header_back1,
.header_back3 {
  height: 40px;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

.dev_boxback {
  height: 100%;
  width: 100%;
  border-radius: 20px;
  position: absolute;
  z-index: 9999;
  animation-name: fadenum;
  animation-duration: 2s;
}

.devbg {
  background-color: rgba(169, 238, 185, 0.185);
}

.devbg1 {
  background-color: rgba(96, 97, 96, 0.185);
}

@keyframes fadenum {
  0% {
    opacity: 1;
  }

  99% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.info-desc {
  width: 25%;
  display: flex;
  flex-direction: column;

  .el-icon-switch-button,
  .el-icon-picture-outline-round,
  .el-icon-refresh,
  .el-icon-key,
  .icon-icon-hongwaishuangjian,
  .el-icon-warning-outline,
  .el-icon-sunny,
  .el-icon-rank,
  .el-icon-sunset,
  .el-icon-orange,
  .el-icon-cloudy,
  .el-icon-pie-chart,
  .icon-fengxiang,
  .el-icon-s-order,
  .el-icon-finished {
    font-size: 38px;
    // box-shadow: 1px 1px 10px rgb(166 166 166);
    cursor: pointer;
  }

  div {
    margin-top: 0.5rem;
  }
}

.el-icon-finished,
.el-icon-setting,
.el-icon-s-order,
.el-icon-edit,
.el-icon-delete {
  font-size: 26px;
  color: rgb(181 187 182);
  cursor: pointer;
  margin-right: 10px;
}

.dev_box {
  position: fixed !important;
  margin: 0 auto;
  z-index: 999;
  left: 0;
  right: 0;
  justify-content: center;
}

.dev_box>div {
  border-radius: 20px;
  background: #fff;
}

.el-input {
  width: 215px;
}

@media screen and (max-width: 1200px) {
  .dev_box>div {
    width: 42%;
    margin-right: 1.5rem;
  }

  &:deep(.el-input) {
    width: 11.3rem;
  }
}

@media screen and (max-width: 1200px) and (min-width: 1200px) {
  .dev_box>div {
    width: 35%;
  }
}

@media screen and (min-width: 1200px) {
  .dev_box>div {
    width: 35%;
  }
}

.all_icon {
  display: flex;
  justify-content: flex-end;
}

.el-icon-close {
  background: #6e6e6e59;
  border-radius: 360px;
  font-size: 26px;
  width: 30px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #333;
}

&:deep(.el-input-number) {
  width: auto;
}

.roomName {
  width: auto;
}

.switchbtn {
  width: 95%;
  margin: 10px;
  float: right;
  display: flex;
  align-items: center;
  // justify-content: end;
  position: relative;
}
</style>
